package com.lbt.javaSE.tanxin;

import java.util.*;

public class Knapsack {
    public static void main(String[] args) {

        find("重量主导",(Thing thing1,Thing thing2)->{
            return thing1.getWeight()-thing2.getWeight();
        });
        System.out.println("--------------------------------");
        find("价值主导",(Thing thing1,Thing thing2)->{
            return thing2.getPrice()-thing1.getPrice();
        });
        System.out.println("---------------------------------");
        find("价值密度主导",(Thing thing1,Thing thing2)->{
            return Double.compare(thing2.getDesp(),thing1.getDesp());
        });


    }


    private static void find(String str, Comparator<Thing> cmp) {
        int count=0;
        Thing[] things= new Thing[]{
                new Thing(35, 10), new Thing(30, 40),
                new Thing(60, 30), new Thing(50, 50),
                new Thing(40, 35), new Thing(10, 40),
                new Thing(25, 30)
        };
        Arrays.sort(things,cmp);
        int cap=150,weight=0,value=0;
        List<Thing> thingList=new LinkedList<>();   //存储放了那些物品
        for(int i = 0; i < things.length&& weight<cap; i++){
            int newWeight=weight+things[i].getWeight();
            if(newWeight<=cap){  //可以装
                weight+=things[i].getWeight();
                value+=things[i].getPrice();
                thingList.add(things[i]);
                count++;
            }
        }
        System.out.println("【"+str+"】"+"容量_:"+cap);
        System.out.println("总价值_："+value);
        System.out.println("重量_:"+weight);
        Iterator<Thing> iterator = thingList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }

}
